import SelectUnit from '../component/SelectUnit';
import { loginPage } from './login';
import JsBridge from '../service/JsBridge';

// 渲染更换账户页
export function changeAccountPage() {


  JsBridge.sendRequest("custom.get", {key: 'yxtLog'}).then(jsrep => {
    const logs = JSON.parse(jsrep.data.value).logs;
    console.log(logs)
    const tpl = `
      <div class="top">
        <p>我的账号</p>
      </div>
      <dl class="list_user">
        ${
          logs.map((item, index) => {
            if (index === 0) {
              return `
                <dd class="on"><a class="btnToLogin" data-data=${JSON.stringify(item)}>
                  <p class="user">${item.UserName}</p>
                  <p class="school_name">${item.UnitName}</p>
                  <img src="img/public/dui.png"/>
                </a></dd>
              `
            }
            return `
              <dd class="on"><a class="btnToLogin" data-data=${JSON.stringify(item)}>
                <p class="user">${item.UserName}</p>
                <p class="school_name">${item.UnitName}</p>
              </a></dd>
            `
          }).join('')
        }
        <dt><a class="lick">选择单位</a></dt>
      </dl>
    `
    $(".right").html(tpl);
    change_acc_event();
  });

}

// 一些事件绑定
function change_acc_event () {
  $(".btnToLogin").click(function() {
    const data = $(this).data("data");
    console.log(data)
    console.log('1234567789')
    loginPage(data)
  })

  const lick=document.getElementsByClassName("lick")[0];
  lick.onclick=function(){                                 //单位选择
      SelectUnit.bindOk((data)=>{
        if(data){
          const {Unit_ID, UnitUrl, UnitName,UnitUrl2} = data;
          console.log(data)
          loginPage({Unit_ID, UnitUrl, UnitName,UnitUrl2});
        }
      })
      SelectUnit.show();
  }
}
